local skel = fk.CreateSkill {
  name = "lb__yueguan",
}

Fk:loadTranslationTable {
  ["lb__yueguan"] = "钺贯",
  --[":lb__yueguan"] = "出牌阶段限一次，你可重铸一名其他角色的一张手牌，若此牌花色本回合首次因此技能进入弃牌堆，你选择一项：<br>"..
  --9.7
  --[":lb__yueguan"] = "出牌阶段限一次，你可以重铸一名角色的一张手牌，若此牌牌名本回合首次因此出现，你选择一项：<br>" ..
  --    "1.将此牌置入你的空装备栏（副类型不同则无技能效果）；<br>" ..
  --    "2.视为对其使用一张【杀】。",
  --9.22
  [":lb__yueguan"] = "出牌阶段限一次，你可以重铸一名角色的一张手牌，若此牌牌名本回合首次因此出现，你选择一项：<br>" ..
      "1.将此牌置入你的空装备栏（副类型不同则无技能效果）；<br>" ..
      "2.视为对其使用一张【杀】。<br>" ..
      "若此角色为你，你可以失去1点体力并重置此技能的发动次数。",

  ["#lb__yueguan"] = "钺贯：你可以选择一名角色的一张手牌，令其重铸之",
  ["#lb__yueguan-ask"] = "钺贯：你可以将 %arg 置入你的空装备栏或视为对 %dest 使用一张【杀】",
  ["#lb__yueguan_slash"] = "视为对 %dest 使用一张【杀】",
  ["#lb__yueguan_lose"] = "钺贯：你可以失去1点体力并重置〖钺贯〗",
  ["@$lb__yueguan-turn"] = "钺贯",

  ["$lb__yueguan1"] = "不避，不悔。",
  ["$lb__yueguan2"] = "无惧，无畏。",
}

skel:addEffect("active", {
  anim_type = "control",
  prompt = "#lb__yueguan",
  can_use = function(self, player)
    return player:usedSkillTimes("lb__yueguan", Player.HistoryPhase) < 1
  end,
  card_filter = Util.FalseFunc,
  min_target_num = 1,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and not to_select:isKongcheng() -- and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local data = effect.extra_data or {}
    local player = effect.from
    local target = effect.tos[1]
    local card = Fk:getCardById(room:askToChooseCard(player, {
      target = target,
      flag = "h",
      skill_name = skel.name,
    }))
    room:recastCard({ card.id }, target, "lb__yueguan")
    --[[
    local suits = player:getTableMark("@lb__yueguan-turn")
    if card.suit == Card.NoSuit or not table.insertIfNeed(suits, card:getSuitString(true)) then return end
    room:setPlayerMark(player, "@lb__yueguan-turn", suits)
    --]]
    --9.7
    local names = player:getTableMark("@$lb__yueguan-turn")
    if not table.insertIfNeed(names, card.trueName) then return end
    room:setPlayerMark(player, "@$lb__yueguan-turn", names)
    local choices = {}
    local choice
    if room:getCardArea(card) == Card.DiscardPile then
      local all_choices = {
        "WeaponSlot",
        "ArmorSlot",
        "DefensiveRideSlot",
        "OffensiveRideSlot",
        "TreasureSlot"
      }
      local subtypes = {
        Card.SubtypeWeapon,
        Card.SubtypeArmor,
        Card.SubtypeDefensiveRide,
        Card.SubtypeOffensiveRide,
        Card.SubtypeTreasure
      }
      for i = 1, #subtypes, 1 do
        if player:hasEmptyEquipSlot(subtypes[i]) then
          table.insert(choices, all_choices[i])
        end
      end
      if #choices > 0 then
        if player:canUseTo(Fk:cloneCard("slash"), target, { bypass_distances = true, bypass_times = true }) then
          table.insert(all_choices, "#lb__yueguan_slash::" .. target.id)
          table.insert(choices, "#lb__yueguan_slash::" .. target.id)
        end
        choice = choices[1]
        if #choices > 1 then
          choice = room:askToChoice(player, {
            choices = choices,
            skill_name = skel.name,
            prompt = "#lb__yueguan-ask::" .. target.id .. ":" .. card:toLogString(),
            all_choices = all_choices,
          })
        end
        if choice ~= "#lb__yueguan_slash::" .. target.id then
          local mapper = {
            [Player.WeaponSlot] = "weapon",
            [Player.ArmorSlot] = "armor",
            [Player.OffensiveRideSlot] = "offensive_ride",
            [Player.DefensiveRideSlot] = "defensive_ride",
            [Player.TreasureSlot] = "treasure",
          }
          if card:getSubtypeString() ~= mapper[choice] then --同步工巧马钧
            local yueguan = Fk:cloneCard(mapper[choice] .. "__yueguan")
            yueguan:addSubcard(card.id)                     --需要id，添加Card[]无效
            room:moveCardIntoEquip(player, yueguan, skel.name, true, player)
          else
            room:moveCardIntoEquip(player, card, skel.name, true, player)
          end
        end
      end
    end
    if #choices == 0 or choice == "#lb__yueguan_slash::" .. target.id then
      local c = Fk:cloneCard("slash")
      c.skillName = (data.lb__tianji and "lb__tianji" or "lb__yueguan")
      if player:canUseTo(c, target, { bypass_distances = true, bypass_times = true }) then
        room:useCard {
          from = player,
          tos = { target },
          card = c,
          extraUse = true,
        }
      end
    end
    if not player.dead and player == target and room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__yueguan_lose" }) then
      room:loseHp(player, 1, skel.name)
      player:setSkillUseHistory(skel.name)
    end
  end,
})

return skel
